package com.sec.android.app.myfiles.module.download;

import android.app.DownloadManager;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.text.TextUtils;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.info.AppConstants;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.util.FileUtils;
import com.sec.android.app.myfiles.util.SemFwWrapper;

import java.io.File;

public class DownloadFileRecord extends FileRecord {
    public static final String EMAIL_DESCRIPTION_HEADER = "(Email)";

    public enum DownloadType {
        DOWNLOAD_BY_NONE,
        DOWNLOAD_BY_BROWSER,
        DOWNLOAD_BY_SIMPLESHARE,
        DOWNLOAD_BY_BLUETOOTH,
        DOWNLOAD_BY_WIFI,
        DOWNLOAD_BY_MESSAGE,
        DOWNLOAD_BY_MTP;
    }

    private int mDownloadId;
    private DownloadType mDownloadType;
    private int mStatus;
    private long mVersion = -1;
    private String mVendor = null;
    private String mSource;
    private String mDescription;
    private boolean mIsDownloadedByEmail = false;

    public DownloadFileRecord(String fullPath) {
        mStorageType = StorageType.Downloads;
        if (fullPath != null) {
            int nLastIndex = fullPath.lastIndexOf('/');

            if (nLastIndex >= 0) {
                mPath = fullPath.substring(0, nLastIndex);
                mName = fullPath.substring(nLastIndex + 1, fullPath.length());
            }
        }
    }

    public DownloadFileRecord(int id, int downloadId, String path, String name, long size, long date, int fileType, String source,
                              String description, int status, int download) {
        super(StorageType.Downloads, id, path, name, size, date, fileType, 0, 0, 0);
        mDownloadId = downloadId;
        mStatus = status;
        mSource = source;
        mDownloadType = DownloadType.values()[download];
        mDescription = description;
        if (mDescription != null && mDescription.startsWith(EMAIL_DESCRIPTION_HEADER)) {
            mIsDownloadedByEmail = true;
            mDescription = mDescription.substring(EMAIL_DESCRIPTION_HEADER.length());
        }
    }

    public int getDownloadId() {
        return mDownloadId;
    }

    public DownloadType getDownloadType() {
        return mDownloadType;
    }

    public int getStatus() {
        return mStatus;
    }

    public String getSource() {
        return mSource;
    }

    public long getVersion(Context context) {
        getDownloadProviderValue(context);
        return mVersion;
    }

    public String getVendor(Context context) {
        getDownloadProviderValue(context);
        return mVendor;
    }

    private void getDownloadProviderValue(Context context) {
        if (mDownloadType == DownloadType.DOWNLOAD_BY_BROWSER) {
            if (mVersion < 0 || mVendor == null) {
                long ids[] = {
                        mDownloadId};

                DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
                DownloadManager.Query query = new DownloadManager.Query().setFilterById(ids);
                try (Cursor c = dm.query(query)) {
                    if (c != null) {
                        if (c.moveToFirst()) {
                            //TODO HYL Download History
                            //mVersion = c.getLong(c.getColumnIndex(DownloadManager.SEM_COLUMN_DD_VERSION_NUMBER));
                            //mVendor = c.getString(c.getColumnIndex(DownloadManager.SEM_COLUMN_DD_VENDOR_NAME));
                        }
                    }
                }
            }
        }
    }

    public String getDescription() {
        return mDescription;
    }

    @Override
    public String getDisplayName(Context context) {
        return context.getString(R.string.downloads);
    }

    @Override
    public boolean isRoot() {
        return TextUtils.isEmpty(mPath);
    }

    @Override
    public boolean isDirectory() {
        return isRoot();
    }

    @Override
    protected int _getId() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    protected Uri _getUri(Context context) {
        // TODO need improve
        return FileUtils.getUriByPath(context, getFullPath());
    }

    @Override
    public boolean exists(Context context) {
        boolean ret;
        String fullPath = getFullPath();
        if (AppConstants.StoragePath.DOWNLOADS.equals(fullPath)
                || AppConstants.StoragePath.DOWNLOAD_HISTORY.equals(fullPath)
                || AppConstants.StoragePath.DOWNLOAD_HISTORY_FROM_OTHERS.equals(fullPath)) {
            ret = true;
        } else {
            File file = SemFwWrapper.file(fullPath);
            ret = file.exists();
        }
        return ret;
    }

    public boolean isDownloadedByEmail() {
        return mIsDownloadedByEmail;
    }
}
